<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>新增管理员</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="{{URL::asset('layui/css/layui.css')}}"  media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>

<form class="layui-form" action="">

    <fieldset class="layui-elem-field site-demo-button" style="margin-top: 20px;">
        <legend>基本信息</legend>
        <div>
            <div class="layui-form-item">
                <label class="layui-form-label">角色</label>
                <div class="layui-input-block">
                    <select id="role_id" name="role_id" lay-filter="aihao">
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">账号</label>
                <div class="layui-input-block">
                    <input type="text" name="username" autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">密码</label>
                <div class="layui-input-block">
                    <input type="password" name="password" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="button" class="layui-btn" lay-submit="" lay-filter="demo1">提交</button>
                <button type="button" id="close" class="layui-btn layui-btn-primary">取消</button>
            </div>
        </div>
    </fieldset>
</form>

<script src="{{URL::asset('layui/layui.js')}}" charset="utf-8"></script>
<script src="{{URL::asset('js/jquery-3.1.1.min.js')}}"></script>
<script>
    layui.use(['form'], function(){
        var form = layui.form
            ,layer = layui.layer;

        //自定义验证规则
        form.verify({
            title: function(value){
                if(value.length < 1){
                    return '标题至少得5个字符啊';
                }
            }
        });

        //一级菜单
        roleList();

        //监听提交
        form.on('submit(demo1)', function(data){
            var f = data.field;
            var where = {"role_id":f.role_id,"username":f.username,"password":f.password};
            var url = "/admin/user/api/add";
            $.post(url,where,function (d) {
                if (d.code==200){
                    layer.msg("管理员添加成功!");
                    setTimeout(function () {
                        window.parent.location.reload();
                    },500)
                }else{
                    layer.msg("管理员添加失败!");
                }
            });

            return false;
        });

        function roleList() {
            var url = "/admin/user/role/all";
            $.get(url,function (d) {
                $.each(d.data,function (i,v) {
                    var jd = '<option value="'+v.id+'">'+v.name+'</option>';
                    $('#role_id').append(jd);
                });
                form.render('select');
            });
        }

    });
</script>

</body>
</html>